<template>
  <div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="(item,index) in allRecomments" :key="index">
        <!-- <div class="pinglun">
          <div class="authorinfo">
            <div class="left">
              <img :src="item.aut_photo" alt style="width:25px;height:25px;border-radius: 50%;" />
              <span class="info">
                <span class="name">{{item.aut_name}}</span>
                <span class="cont">{{item.content}}</span>
                <span class="time">
                  <span>{{item.pubdate | format}}</span>
                  <van-button round type="default" @click="showPopup">回复1</van-button>
                </span>
              </span>
            </div>
            <div class="right">
              <van-icon
                slot="icon"
                :name="item.is_liking?'good-job':'good-job-o'"
                :class="{active:item.is_liking}"
                style="font-size:14px;"
                @click="likeFunc(item.is_liking,item.com_id)"
              />
              <span style="font-size:11px;margin-left:5px">赞</span>
            </div>
          </div>
        </div>-->
        <!-- 评论列表每一项 -->
        <recomment-item :item="item" @comment-reply="reply"></recomment-item>
        <!-- 评论列表每一项 -->
      </van-cell>
    </van-list>
  </div>
</template>
<script>
import {
  getRecomments_api,
  commentliking_api,
  nocommentliking_api,
} from "@/api/user.js";
import recommentItem from "./recomment-item";
export default {
  props: {
    artId: {
      type: [Number, String],
    },
    allRecomments: {
      type: [Array, Object, String],
      default: () => [], // 默认为数组
    },
    attitude: {
      type: [Number, String],
    },
  },
  data() {
    return {
      loading: false,
      finished: false,
      //   allRecomments: "",
      type: "a",
      offset: null,
      limit: 10,
      //   show: false,
    };
  },
  created() {
    // this.onLoad();
  },
  methods: {
    async onLoad() {
      // 获取评论
      const { data: res } = await getRecomments_api({
        type: this.type,
        source: this.artId,
        offset: this.offset,
        limit: this.limit,
      });
      console.log(res);
      this.allRecomments.push(...res.data.results);
      //   console.log(this.allRecomments, 123);
      this.$emit("total", res.data.total_count);
      // 加载状态结束
      this.loading = false;
      if (res.data.results.length) {
        this.offset = res.data.last_id;
      } else {
        this.finished = true;
      }
    },
    // 点赞
    async likeFunc(liking, id) {
      if (!liking) {
        // liking = !liking;
        // 点赞
        const { data: res } = await commentliking_api(id);
        this.$toast("点赞成功");
      } else {
        // 不点赞
        const { data: res } = await nocommentliking_api(id);
        this.$toast("取消点赞");
      }
      liking = !liking;
    },
    // 点击回复显示弹出框
    showPopup() {
      this.show = true;
    },
    // 回复
    reply(val) {
      this.$emit("comment-reply", val);
    },
  },
  components: {
    recommentItem,
  },
};
</script>
<style lang='less' scoped>
.active {
  color: #e5645f;
}
.authorinfo {
  height: 120px;
  display: flex;
  justify-content: space-between;
  padding: 20px 0;
  .left {
    display: flex;
    img {
      vertical-align: middle;
      margin-right: 20px;
    }
    .name {
      color: #6bb4eb;
    }
    .info {
      font-size: 24px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
  }
  .van-button {
    margin-left: 20px;
    width: 150px;
    height: 50px;
    .van-button__text {
      font-size: 12px;
      color: #000;
    }
  }
}
</style>